using System;
using Microsoft.DirectX.Direct3D;
using SharpWind.Helpers;

namespace SharpWind.Graphics
{
	public class RenderTarget
	{
		#region Fields

		private RenderToSurface rts = null;

		private Texture dxTexture = null;

		private Surface dxSurface = null;

		private int width;

		private int height;

		#endregion

		#region Constructor

		public RenderTarget( int width, int height )
		{
			this.rts = new RenderToSurface( Engine.Instance.Graphics.Device, width, height, Format.X8R8G8B8,
											true, DepthFormat.D16 );

			this.dxTexture = new Texture( Engine.Instance.Graphics.Device, width, height, 1, Usage.RenderTarget,
										  Format.X8R8G8B8, Pool.Default );

			this.dxSurface = this.dxTexture.GetSurfaceLevel( 0 );
		}

		#endregion

		#region Method

		public void RenderToTexture( RenderCallback renderCallback )
		{
			this.rts.BeginScene( this.dxSurface );
			Engine.Instance.Graphics.Device.Clear( ClearFlags.Target | ClearFlags.ZBuffer, 0, 1f, 0 );

			renderCallback();

			this.rts.EndScene( Filter.None );
		}

		#endregion

		#region Properties

		internal Texture DxTexture
		{
			get
			{
				return this.dxTexture;
			}
		}

		public int Width
		{
			get
			{
				return this.width;
			}
		}

		public int Height 
		{
			get
			{
				return this.height;
			}
		}

		#endregion
	}
}